# -*- coding: utf-8 -*-
# ****************************************************
# Author: yizuoliang
# Create: 2024/6/24
# Last Modified: 2024/6/24
# Last Editor:
# Description: 统一异常处理
# ***************************************************
from applications.common.app_exception import AppException
from applications.common.constants.status_codes import ResponseCode
from applications.common.utils.response import api_response


def init_error(app):
    @app.errorhandler(AppException)
    def internal_app_exception(e):
        return api_response(e.code, e.message)

    @app.errorhandler(401)
    def page_not_found(e):
        return api_response(ResponseCode.UNAUTHORIZED.code, ResponseCode.UNAUTHORIZED.message)

    @app.errorhandler(403)
    def page_not_found(e):
        return api_response(ResponseCode.FORBIDDEN.code, ResponseCode.FORBIDDEN.message)

    @app.errorhandler(404)
    def page_not_found(e):
        return api_response(ResponseCode.NOT_FOUND.code, ResponseCode.NOT_FOUND.message)

    @app.errorhandler(Exception)
    def internal_server_error(e):
        return api_response(ResponseCode.INTERNAL_SERVER_ERROR.code, ResponseCode.INTERNAL_SERVER_ERROR.message)
